package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Resource;
import com.itheima.mapper.ResourceMapper;
import com.itheima.service.IResourceService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {
    @Autowired
    ResourceMapper resourceMapper;

    @Override
    public PageBeanVo findByConditionAndPage(Integer pageNum, Integer pageSize, String nameKeyword, String urlKeyword, String categoryId) {

        //添加查询条件
        LambdaQueryWrapper<Resource> qw=new LambdaQueryWrapper<>();
        if(nameKeyword!=null){
            qw.like(Resource::getName,nameKeyword);
        }
        if(urlKeyword!=null){
            qw.like(Resource::getUrl,urlKeyword);
        }
        if(categoryId!=null){
            qw.eq(Resource::getCategoryId,categoryId);
        }

        //添加分页条件
        Page<Resource> page=new Page<>(pageNum,pageSize);

        resourceMapper.selectPage(page,qw);

        //封装进PageBeanvVo对象
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());

    }

    @Override
    public List<Resource> findAll() {
        QueryWrapper<Resource> qw=new QueryWrapper<>();
        return resourceMapper.selectList(qw);
    }

    @Override
    public void updateResource(Resource resource) {
        resourceMapper.updateById(resource);
    }

    @Override
    public Resource findById(Long id) {
        return resourceMapper.selectById(id);
    }

    @Override
    public void saveResource(Resource resource) {
        resourceMapper.insert(resource);
    }

    @Override
    public void deleteResource(Long id) {
        resourceMapper.deleteById(id);
    }
}
